<?php

require('main.php');

class Article extends Main {
	
	function Article() {
		
		parent::Main();
		$this->lib('message');
		$this->database();	
		$this->lib('pagination'); //分页;

	}
	
	function index() {
		$total  = $this->db->count('article');
		$config = array(
			'total_row'	=> $total,
		  	'page_size' => 10,
			'url'		=> site_url('manage/article')
		);
		$this->pagination->init($config);
		$article_list = $this->db->find("select * from article order by created desc limit "
			.$this->pagination->start_row.",".$this->pagination->page_size);
		
		$data = array(
		      'article_list'	=> $article_list
		);
		
		$this->my_view('manage/article/article_list',$data);	
	}

	function init_add() {
		$this->my_view('manage/article/article_add',$this->languageList() );
	}
	

	function add() {
		$id = $this->db->insert('article',$this->_fetch_form());
		if( $id ) {
			$this->message->set("恭喜你,增加文章成功");
			$this->my_view('manage/article/article_success');
		}
		else { 
            $this->message->set("抱歉,添加文章失败！");
			$this->my_view('manage/article/article_add',$this->languageList());
		}
	}
	
	
	/**
	 * 文章的删除delete 
	 * 
	 */
	function delete() {     
		$id_sql = join(",", $_POST['id']);
		$result = $this->db->delete('article', "id in($id_sql)");
		if($result){
			$this->message->set("恭喜你,删除成功！");
		}
		else{
			$this->message->set("抱歉,删除失败！");	  	
		}
		$this->index();
	}
	
	function init_update($id) { 		 
         $data = array(
         	'language_list' =>$this->db->find_all('languages'),
            'article' 		=>$this->db->find_by_id('article', $id)
         );
         $this->my_view('manage/article/article_update',$data);
	}
	
	/**
	 * 
	 * 
	 * @文章更新成功的方法
	 * 调用了articleAdd() 方法
	 * 
	 * 
	 */
	function update() {   
		 $article_id_row =  $this->db->find("select * from article where id=".$_POST['id'] ); 
         $language_list = $this->db->find("select * from languages");  
         $data = array(
                 'language_list' =>$language_list,
                 'article_id_row' =>$article_id_row
         );
         
		$result = $this->db->update('article', $this->_fetch_form(), 'id=' . $_POST['id']);
		if($result ){
			 $this->message->set("恭喜你,更新成功！");
			 $this->my_view('manage/article/article',$this->articleList() ); 
		}
		else{
		     $this->message->set("抱歉,更新失败！");
		     $this->my_view('manage/article/article_update',$data); 	
			
		}
		
	}

	
	function languageList() {
		$language_list = $this->db->find("select * from languages");
		return array(
			'language_list'=>$language_list
		);
	}
	

	function articleList() {
		$total  = $this->db->count('article');
		$config = array(
		  'total_row'=>$total,
		  'page_size' =>10
		);
		$this->pagination->init($config);
		$article_list = $this->db->find("select * from article order by created desc limit "
			.$this->pagination->start_row.",".$this->pagination->page_size);
		
		$data = array(
		      'article_list'	=> $article_list
		);
		
		return $data; 
	}
	
	/**
	 *  
	 * @公用插入语句函数
	 * extract中带hidden影藏的$id;  
	 * 被 add(), update() 应用； 
	 * 
	 */
	function _fetch_form() {
		return array(
			'language'   	=> $_POST['language'],
		    'name'      	=> $_POST['title'],
			'url_pattern'	=> $_POST['url_pattern'],
		    'content'    	=> $_POST['content'],
			'summary'    	=> $_POST['summary'],
			'title'         => $_POST['title'],
		    'keywords'      => $_POST['keywords'],
		    'description'	=> $_POST['description']       
        );
	}
	 
		 
}


?>